<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 实现透明单例类
      /* 
      例子：使用 CreateDiv 单例类，它的作用是负责在页面中创建唯一的 div 节点
      */
      var CreateDiv = (function () {
        var instance

        var CreateDiv = function (html) {
          if (instance) {
            return instance
          }
          this.html = html
          this.init()
          return (instance = this)
        }

        CreateDiv.prototype.init = function () {
          var div = document.createElement('div')
          div.innerHTML = this.html
          document.body.appendChild(div)
        }

        return CreateDiv // 返回真正的单例类
      })()

      var a = new CreateDiv('wangxin1')
      var b = new CreateDiv('wangxin2')

      console.log(a === b) // true
    </script>
  </body>
</html>
